package org.black.phoneUtility.sms.schedule.app.fragment;

import org.black.phoneUtility.R;
import org.black.phoneUtility.sms.schedule.provider.SmsScheduleContract;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.NumberPicker;

public class SmsScheduleRepeatEveryDialogFragment extends DialogFragment {
	public static final String TAG = SmsScheduleRepeatEveryDialogFragment.class
			.getName();
	public static final String LOG_TAG = SmsScheduleRepeatEveryDialogFragment.class
			.getName();
	public static final String ARGUMENTS_REPEAT_TYPE = "arguments_repeat_type";
	public static final String ARGUMENTS_REPEAT_EVERY_VALUE = "arguments_repeat_every_value";

	public interface SmsScheduleRepeatEveryDialogInterface {
		public void onSmsScheduleRepeatEveryDialogPositiveClick(int repeatType,
				int number);

		public void onSmsScheduleRepeatEveryDialogNegativeClick();
	}

	private SmsScheduleRepeatEveryDialogInterface smsScheduleRepeatEveryDialogInterface;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			smsScheduleRepeatEveryDialogInterface = (SmsScheduleRepeatEveryDialogInterface) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement SmsScheduleRepeatEveryDialogInterface");
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		super.onCreateDialog(savedInstanceState);
		Bundle bundle = getArguments();
		int repeatType = bundle.getInt(ARGUMENTS_REPEAT_TYPE);
		int repeatEvery = bundle.getInt(ARGUMENTS_REPEAT_EVERY_VALUE);
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		Log.d(LOG_TAG, "repeatType: " + repeatType + ", repeatEvery: "
				+ repeatEvery);

		LayoutInflater inflater = getActivity().getLayoutInflater();
		View view = inflater.inflate(R.layout.sms_schedule_repeat_every_dialog,
				null);
		NumberPicker numberPicker = (NumberPicker) view
				.findViewById(R.id.smsScheduleRepeatEveryNumberPicker);
		switch (repeatType) {
		case SmsScheduleContract.SmsSchedules.REPEAT_TYPE_EVERY_MINUTE:
			numberPicker.setMinValue(1);
			numberPicker.setMaxValue(60);
			if (repeatEvery < 1) {
				repeatEvery = 1;
			} else if (repeatEvery > 60) {
				repeatEvery = 60;
			}
			break;
		case SmsScheduleContract.SmsSchedules.REPEAT_TYPE_HOURLY:
			numberPicker.setMinValue(1);
			numberPicker.setMaxValue(24);
			if (repeatEvery < 1) {
				repeatEvery = 1;
			} else if (repeatEvery > 24) {
				repeatEvery = 24;
			}
			break;
		case SmsScheduleContract.SmsSchedules.REPEAT_TYPE_DAILY:
			numberPicker.setMinValue(1);
			numberPicker.setMaxValue(7);
			if (repeatEvery < 1) {
				repeatEvery = 1;
			} else if (repeatEvery > 7) {
				repeatEvery = 7;
			}
			break;
		case SmsScheduleContract.SmsSchedules.REPEAT_TYPE_WEEKLY:
			numberPicker.setMinValue(1);
			numberPicker.setMaxValue(52);
			if (repeatEvery < 1) {
				repeatEvery = 1;
			} else if (repeatEvery > 52) {
				repeatEvery = 52;
			}
			break;
		case SmsScheduleContract.SmsSchedules.REPEAT_TYPE_MONTHLY:
			numberPicker.setMinValue(1);
			numberPicker.setMaxValue(12);
			if (repeatEvery < 1) {
				repeatEvery = 1;
			} else if (repeatEvery > 12) {
				repeatEvery = 12;
			}
			break;
		case SmsScheduleContract.SmsSchedules.REPEAT_TYPE_YEARLY:
			numberPicker.setMinValue(1);
			numberPicker.setMaxValue(10);
			if (repeatEvery < 1) {
				repeatEvery = 1;
			} else if (repeatEvery > 10) {
				repeatEvery = 10;
			}
			break;
		default:
			break;
		}
		numberPicker.setValue(repeatEvery);
		builder.setView(view)
				.setPositiveButton(android.R.string.ok,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								smsScheduleRepeatEveryDialogInterface
										.onSmsScheduleRepeatEveryDialogPositiveClick(
												getArguments().getInt(
														ARGUMENTS_REPEAT_TYPE),
												((NumberPicker) getDialog()
														.findViewById(
																R.id.smsScheduleRepeatEveryNumberPicker))
														.getValue());

							}
						})
				.setNegativeButton(android.R.string.cancel,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {

							}
						});
		return builder.create();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		this.smsScheduleRepeatEveryDialogInterface = null;
	}

}
